home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 December / Chip_2000-12_cd1.bin / zkuste / MacOS / Files / Windox.sit / Windox 1.0 / Source / Windox.c next >
C/C++ Source or Header  |  1993-06-29  |  6KB  |  213 lines

  1. // SuperGrow.c ⌐ 1992 Hugues Marty
  2.  
  3. //*****************************************************************************
  4.  
  5. #define findWindowTrap    0xa92c
  6. #define growWindowTrap    0xA92B
  7.  
  8. #define baseIcon        128
  9.  
  10. //*****************************************************************************
  11.  
  12. pascal long BWGrowWindow(WindowPtr theWindow, Point startPt, Rect *sizeRect);
  13. pascal void ShowInit(short, short);
  14.  
  15. long        oldGrowWindow;
  16. long        gneResult;
  17. Pattern        grayPat = {0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55};
  18.  
  19. //*****************************************************************************
  20.  
  21. main()                            // main must be at top of resource
  22. {
  23.     asm {
  24.     
  25. // setup code here (INIT time)
  26.  
  27.             clr.b    -(sp)        // Test mouse down
  28.             Button
  29.             tst.b    (sp)+
  30.             bne.s    @dontLoad
  31.             
  32.             sub        #sizeof(KeyMap),sp        // Test shift key
  33.             pea        (sp)
  34.             GetKeys
  35.             btst    #0,7(sp)
  36.             add        #sizeof(KeyMap),sp
  37.             bne.s    @dontLoad
  38.             
  39. // keep code in system heap
  40.             
  41.             lea        main,a0        // float ourselves in system heap
  42.             move.l    a0,a4
  43.             RecoverHandle        // call recoverhandle
  44.             move.l    a0,-(sp)    // to get handle
  45.             DetachResource        // and detachresouce to release ourselves
  46.             
  47.             move.l    #growWindowTrap,d0
  48.             GetToolTrapAddress
  49.             move.l    a0,oldGrowWindow
  50.             lea        BWGrowWindow,a0
  51.             move.l    #growWindowTrap,d0
  52.             SetToolTrapAddress
  53.  
  54.             move.w    #baseIcon,-(sp)
  55. @away        move.w    #-1,-(sp)
  56.             bsr        ShowInit
  57.             rts
  58. @dontLoad    move.w    #baseIcon+1,-(sp)
  59.             bra.s     @away
  60.     }
  61. }
  62.  
  63. typedef struct {
  64.     long    top, left, bottom, right;
  65. } LongRect;
  66.  
  67. pascal long BWGrowWindow(register WindowPtr theWindow, Point startPt, Rect *sizeRect)
  68. {
  69.     long result;
  70.     Rect oldRect, newRect, curRect;
  71.     GrafPtr oldPort;
  72.     GrafPtr wMgrPort;
  73.     PenState penState;
  74.     Rect bmRect;
  75.     Point newPt;
  76.     char theKeys[16]  /*instead of KeyMap*/;
  77.     LongRect sizeLeft, sizeRight;    // Bounds
  78.     Boolean cmdDown, optDown, shiftDown;
  79.     
  80.     asm {
  81.             movem.l    d1-d7/a1-a5,-(sp)    // save registers
  82.             lea        main,a4                // setup a4 for globals
  83.     }
  84.     
  85.     // Now, do our stuff...
  86.     
  87.     // Prepare port
  88.     
  89.     GetPort(&oldPort);
  90.     GetWMgrPort(&wMgrPort);
  91.     SetPort(wMgrPort);
  92.     
  93.     SetClip(GetGrayRgn());
  94.     ClipAbove(theWindow);
  95.  
  96.     GetPenState(&penState);
  97.     PenNormal();
  98.     PenMode(notPatXor);
  99.     PenPat(grayPat);
  100.     
  101.     newRect = theWindow->portRect;
  102.     bmRect = theWindow->portBits.rowBytes < 0 ?
  103.                 ( **( ((CGrafPtr) theWindow)->portPixMap) ).bounds :
  104.                 theWindow->portBits.bounds;
  105.     OffsetRect(&newRect, -bmRect.left, -bmRect.top);
  106.     oldRect = curRect = newRect;
  107.     // newRect est maintenant le rectangle de la fenÉtre en coord. globales.
  108.     
  109.     startPt.v = oldRect.bottom - startPt.v;
  110.     startPt.h = oldRect.right - startPt.h;
  111.     sizeRight.top    = (long) topLeft(oldRect).v + sizeRect->top - startPt.v;
  112.     sizeRight.left   = (long) topLeft(oldRect).h + sizeRect->left - startPt.h - 1;
  113.     sizeRight.bottom = (long) topLeft(oldRect).v + sizeRect->bottom - startPt.v;
  114.     sizeRight.right  = (long) topLeft(oldRect).h + sizeRect->right - startPt.h - 1;
  115.     sizeLeft.top     = (long) botRight(oldRect).v - sizeRect->bottom;
  116.     sizeLeft.left    = (long) botRight(oldRect).h - sizeRect->right + 1;
  117.     sizeLeft.bottom  = (long) botRight(oldRect).v - sizeRect->top;
  118.     sizeLeft.right   = (long) botRight(oldRect).h - sizeRect->left + 1;
  119.     
  120.     asm { bsr @paintWStruct }
  121.  
  122.     while (WaitMouseUp()) {
  123.         /* loop here */
  124.         GetMouse(&newPt);
  125.         
  126.         GetKeys(theKeys);
  127.         cmdDown = theKeys[6] & 0x80;
  128.         shiftDown = theKeys[7] & 0x01;
  129.         optDown = theKeys[7] & 0x04;
  130.         
  131.         curRect = oldRect;
  132.         if (newPt.h < topLeft(oldRect).h) {
  133.             if ((! cmdDown) && newPt.h < sizeLeft.left) newPt.h = sizeLeft.left;
  134.             curRect.left = newPt.h;
  135.         } else if (optDown && newPt.h < botRight(oldRect).h) {
  136.             if (newPt.h > sizeLeft.right) newPt.h = sizeLeft.right;
  137.             curRect.left = newPt.h;
  138.         } else {
  139.             if (newPt.h < sizeRight.left) newPt.h = sizeRight.left;
  140.             if ((! cmdDown) && newPt.h > sizeRight.right) newPt.h = sizeRight.right;
  141.             curRect.right = newPt.h + startPt.h;
  142.         }
  143.         if (newPt.v < topLeft(oldRect).v) {
  144.             if ((! cmdDown) && newPt.v < sizeLeft.top) newPt.v = sizeLeft.top;
  145.             curRect.top = newPt.v;
  146.         } else if (shiftDown && newPt.v < botRight(oldRect).v) {
  147.             if (newPt.v > sizeLeft.bottom) newPt.v = sizeLeft.bottom;
  148.             curRect.top = newPt.v;
  149.         } else {
  150.             if (newPt.v < sizeRight.top) newPt.v = sizeRight.top;
  151.             if ((! cmdDown) && newPt.v > sizeRight.bottom) newPt.v = sizeRight.bottom;
  152.             curRect.bottom = newPt.v + startPt.v;
  153.         }
  154.         
  155.         if (! EqualRect(&newRect, &curRect)) {
  156.             asm { bsr @paintWStruct }
  157.             newRect = curRect;
  158.             asm { bsr @paintWStruct }
  159.         }
  160.     }
  161.     
  162.     asm { bsr @paintWStruct }
  163.     
  164.     // If window must move, do it
  165.     if (*(long*)&newRect != *(long*)&oldRect)
  166.         MoveWindow(theWindow, newRect.left, newRect.top, false);
  167.     result = EqualRect(&oldRect, &newRect) ?
  168.                 0 :
  169.                 (long) (newRect.bottom - newRect.top) << 16 |
  170.                 ((long) (newRect.right - newRect.left) & 0xffff);
  171.     
  172.     SetPenState(&penState);
  173.     SetPort(oldPort);
  174.     asm {
  175.             movem.l    (sp)+,d1-d7/a1-a5    // Restore everything
  176.     }
  177.     return result;
  178.  
  179. /******************/
  180.  
  181. @paintWStruct:
  182.     asm {
  183.         sub            #sizeof(long)+sizeof(short),sp
  184.                                             // room for result & variant
  185.         move.l        theWindow,-(sp)            //
  186.         GetWVariant                            // GetWVariant(theWindow)
  187.         move.l        theWindow,-(sp)            // theWindow
  188.         move.w        #wGrow,-(sp)            // message = wGrow
  189.         pea            newRect                    // param = &newRect
  190.                                             // windowProc (Handle)
  191.         movea.l     OFFSET(WindowRecord,windowDefProc)(theWindow),a0
  192.  
  193.         tst.l        (a0)
  194.         bne.s        @1
  195.         move.l        a0,d0
  196.         StripAddress
  197.         move.l        d0,-(sp)
  198.         LoadResource
  199.  
  200. @1        HLock
  201.         movea.l     (a0),a0                    // a0 = &wdef
  202.  
  203.         jsr         (a0)
  204.                     // wdef(varCode, theWindow, wGrow, &newRect) : longint
  205.  
  206.         movea.l     OFFSET(WindowRecord,windowDefProc)(theWindow),a0
  207.                     // unlock wdef handle !
  208.         HUnlock
  209.         move.l        (sp)+,d0                // pop wdef result code
  210.         rts
  211.      }
  212. }
  213.